/* 
 * File:   MutexAble.h
 * Author: bastien
 *
 * Created on 12 octobre 2010, 11:57
 */

#ifndef MUTEXABLE_H
#define	MUTEXABLE_H

#include <pthread.h>


class MutexAble {
public:
    MutexAble();
    MutexAble(const MutexAble& orig);
    virtual ~MutexAble();

    inline void lock() { pthread_mutex_lock(&mymutex);}
    inline void unlock() {pthread_mutex_trylock(&mymutex);}
    inline void trylock() {pthread_mutex_unlock(&mymutex);}

protected:

    pthread_mutex_t * getMutex() {return &mymutex;}

    class Locker{
    public:
        Locker(pthread_mutex_t* m){
            mutex = m;
            pthread_mutex_lock(mutex);
        }

        ~Locker(){
            pthread_mutex_unlock(mutex);
        }
    private:
        Locker();
        Locker(const Locker&);

        pthread_mutex_t * mutex;
    };

private:

    pthread_mutex_t mymutex;
    pthread_mutexattr_t myattr;


};

#define Lock MutexAble::Locker locker(this->getMutex())

#endif	/* MUTEXABLE_H */

